//
//  LLGoodsBasicViewModel.m
//  lrlzapp
//
//  Created by dl on 16/3/2.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLGoodsBasicViewModel.h"

@interface LLGoodsBasicViewModel ()

@property(nonatomic, assign, readwrite) BOOL isTmallGoods;
@property(nonatomic, assign, readwrite) BOOL isSoldOut;

@property(nonatomic, strong, readwrite) NSString *goodsTypeStr;
@property(nonatomic, strong, readwrite) NSAttributedString *titleAttr;
@property(nonatomic, strong, readwrite) NSAttributedString *priceAttr;
@property(nonatomic, assign, readwrite) CGFloat priceLabelWidth;
@property(nonatomic, strong, readwrite) NSString *salesStr;

@property(nonatomic, strong, readwrite) NSArray *tagViewModels;

@end

@implementation LLGoodsBasicViewModel

- (BOOL)isTmallGoods {
    return [self.goodsBasic.status integerValue] != 7;
}

- (BOOL)isSoldOut {
    return [self.goodsBasic.stockCount integerValue] == 0;
}

- (NSString *)goodsTypeStr {
    if (self.isTmallGoods) {
        return @"";
    } else {
        return @"会员";
    }
}

- (NSAttributedString *)titleAttr {
    if (_titleAttr == nil) {
        NSMutableAttributedString *titleAttr = [[NSMutableAttributedString alloc] initWithString:self.goodsBasic.title];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];

        if (!self.isTmallGoods) {
            style.firstLineHeadIndent = 30;
        } else {
            style.firstLineHeadIndent = 0;
        }

        [titleAttr addAttributes:@{NSFontAttributeName : SUIT_FONT(13),
                        NSForegroundColorAttributeName : UI_COLOR_FONT_BLACK,
                        NSParagraphStyleAttributeName : style}
                           range:NSMakeRange(0, titleAttr.length)];

        _titleAttr = [[NSAttributedString alloc] initWithAttributedString:titleAttr];
    }

    return _titleAttr;
}

- (NSAttributedString *)priceAttr {
    if (_priceAttr == nil) {
        NSString *priceStr = [NSString stringWithFormat:@"￥%.2f", [self.goodsBasic.curPrice doubleValue]];
        NSMutableAttributedString *priceAttr = [[NSMutableAttributedString alloc] initWithString:priceStr];
        [priceAttr addAttribute:NSFontAttributeName value:SUIT_FONT_320(18) range:NSMakeRange(0, priceStr.length - 3)];
        [priceAttr addAttribute:NSFontAttributeName value:SUIT_FONT_320(14) range:NSMakeRange(priceStr.length - 3, 3)];
        [priceAttr addAttribute:NSForegroundColorAttributeName value:UI_COLOR_RED range:NSMakeRange(0, priceStr.length)];

        CGSize priceSize = [TTTAttributedLabel sizeThatFitsAttributedString:priceAttr withConstraints:CGSizeMake(SUIT_W(175), 0) limitedToNumberOfLines:0];

        _priceAttr = [[NSAttributedString alloc] initWithAttributedString:priceAttr];
        _priceLabelWidth = priceSize.width;
    }
    return _priceAttr;
}

- (CGFloat)priceLabelWidth {
    if (_priceLabelWidth == 0) {
        [self priceAttr];
    }
    return _priceLabelWidth;
}

- (NSString *)salesStr {
    if (_salesStr == nil) {
        NSString *salesStr = [NSString stringWithFormat:@"%@人购买", [[NSString stringWithFormat:@"%@", self.goodsBasic.sales] humanSalesCnt]];
        _salesStr = salesStr;
    }
    return _salesStr;
}

- (NSArray *)tagViewModels {
    NSMutableArray *tags = [NSMutableArray arrayWithCapacity:3];
    CGFloat preTagBtnRightX = SUIT_W(5);
    for (int i = 0; i < 3; i++) {
        LLgoodsTagViewModel *tag = [LLgoodsTagViewModel modelWithTagStr:nil tagFrame:CGRectZero];
        if (self.goodsBasic.tags.count > i) {
            NSString *tagStr = self.goodsBasic.tags[i];
            CGSize tagStrSize = [tagStr sizeWithAttributes:@{NSFontAttributeName : SUIT_FONT_320(10)}];

            CGRect tagFrame = CGRectMake(preTagBtnRightX, SUIT_H(5), tagStrSize.width + SUIT_W(10) * 2, SUIT_H(15));
            preTagBtnRightX += tagStrSize.width + SUIT_W(10) * 2 + SUIT_W(5);

            tag = [LLgoodsTagViewModel modelWithTagStr:tagStr tagFrame:tagFrame];
        }
        [tags addObject:tag];
    }
    return tags;
}

@end

@interface LLgoodsTagViewModel ()
@property(nonatomic, assign, readwrite) BOOL hiden;
@property(nonatomic, strong, readwrite) NSString *tagStr;
@property(nonatomic, assign, readwrite) CGRect tagFrame;
@end

@implementation LLgoodsTagViewModel

+ (LLgoodsTagViewModel *)modelWithTagStr:(NSString *)tagStr tagFrame:(CGRect)tagFrame {
    LLgoodsTagViewModel *vm = [[LLgoodsTagViewModel alloc] init];
    vm.tagStr = tagStr;
    vm.tagFrame = tagFrame;
    vm.hiden = CGRectEqualToRect(tagFrame, CGRectZero);
    return vm;
}

@end
